﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;

using Actuals.CoreDomain.Business;
using Actuals.Business;
using Actuals.CoreDomain;


namespace Actuals
{
    public partial class AddEmployee : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!Page.IsPostBack)
            {
                if (Request.QueryString["Mode"] != null)
                {

                    string _mode = Request.QueryString["Mode"].ToString();
                    ViewState["Mode"] = _mode;
                    string _employeeId = Request.QueryString["EmployeeId"].ToString();
                    hidEmployeeId.Value = _employeeId;
                    PopulateEmployee(int.Parse(_employeeId));
                }
            }
        }

        protected void PopulateEmployee(int employeeId)
        {
            IBusinessEmployee businessEmployee = new BusinessEmployee();
            IEmployee employee = businessEmployee.GetEmployee(employeeId);

            txtFirstName.Text = employee.FirstName;
            txtLastName.Text = employee.LastName;
            txtNickName.Text = employee.NickName;
            if (employee.DOB != null)
                txtDOB.Text = employee.DOB.Value.Date.ToString("MM-dd-yyyy");
            ddlRole.SelectedValue = employee.RoleId.ToString();



        }
        protected void btnSave_Click(object sender, EventArgs e)
        {

            try
            {

                IEmployee employee = new Employee();

                employee.FirstName = txtFirstName.Text;
                employee.LastName = txtLastName.Text;
                employee.NickName = txtNickName.Text;
                if (!string.IsNullOrEmpty(txtDOB.Text))
                    employee.DOB = DateTime.ParseExact(txtDOB.Text,"MM-dd-yyyy",new CultureInfo("en-US"));
                employee.RoleId = int.Parse(ddlRole.SelectedValue);

                IBusinessEmployee businessEmployee = new BusinessEmployee();
                if (ViewState["Mode"] != null)
                {
                    employee.Id = int.Parse(hidEmployeeId.Value);
                    businessEmployee.UpdateEmployee(employee);
                }
                else
                businessEmployee.AddEmployee(employee);

                lblMsg.Text = "Saved!";
            }
            catch (Exception ex)
            {
                lblMsg.Text = ex.Message;
            }




        }

    }
}